import React from 'react';
import { Button, Form, Input, InputNumber, Modal } from 'antd';
import Constants from '../../../utils/Constants';
import moment from 'moment'
const R = require('ramda');
const FormItem = Form.Item;
const buyerAccountDetail = Form.create({ wrappedComponentRef: true })(class BuyerAccountDetail extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            open: false,
            disabled: false,
            dataSource: {}
        }
    }

    handleCancel = () => {
        this.props.form.resetFields();
        this.setState({ open: false, dataSource: {} });
    }

    handleOpen = (dataSource) => {
        this.setState({ open: true, dataSource });
    }

    handleSave = () => {
        this.props.form.validateFields({ force: true }, (errors, values) => {
            if (errors) return;
            values.buyerId = this.state.dataSource.buyerId;
            values.id = this.state.dataSource.id ? this.state.dataSource.id : "new_" + new Date().getTime();
            values.creatorName = this.state.dataSource.id ? this.state.dataSource.creatorName : sessionStorage.getItem("userName");//默认当前登陆人
            values.createTime = this.state.dataSource.id ? this.state.dataSource.createTime : moment().format(Constants.DATEFORMAT);//默认今天
            if (this.props.handleAddOrUpdateAccount) this.props.handleAddOrUpdateAccount(values);
            this.handleCancel();
        })
    }

    render() {
        let dataSource = R.isNil(this.state.dataSource) ? {} : this.state.dataSource;
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 14 } };

        let footer = [
            <Button key="save" size="large" loading={this.props.loading} onClick={this.handleSave.bind(this)} style={{ display: this.state.disabled ? 'none' : 'inline-block' }}>保存</Button>,
            <Button key="cancel" size="large" loading={this.props.loading} onClick={this.handleCancel.bind(this, true)}>{this.state.disabled ? "关闭" : "取消"}</Button>
        ];

        return (
            <Modal maskClosable={false} visible={this.state.open} title={"付汇账号详情"} width={600}
                onCancel={this.handleCancel.bind(this)} footer={footer} closable={false}>
                <Form>
                    <FormItem {...formItemLayout} label="付汇银行" hasFeedback>
                        {getFieldDecorator('bankName',
                            {
                                rules: [{ required: true, message: '请输入付汇银行' }],
                                initialValue: dataSource.bankName
                            })(
                            <Input maxLength="200" style={{ width: "100%" }} disabled={this.state.disabled} />
                            )}
                    </FormItem>
                    <FormItem {...formItemLayout} label="付汇账号名称" hasFeedback>
                        {getFieldDecorator('accountName',
                            {
                                rules: [
                                    { required: true, message: '请输入付汇账号名称' },
                                ],
                                initialValue: dataSource.accountName
                            })(

                            <Input maxLength="200" style={{ width: "100%" }} disabled={this.state.disabled} />
                            )}
                    </FormItem>
                    <FormItem {...formItemLayout} label="付汇账号" hasFeedback>
                        {getFieldDecorator('account',
                            {
                                rules: [
                                    { required: true, message: '请输入付汇账号' },
                                ],
                                initialValue: dataSource.account
                            })(
                            <Input maxLength="100" style={{ width: "100%" }} disabled={this.state.disabled} />
                            )}
                    </FormItem>
                </Form>
            </Modal>
        );
    }
})

export default buyerAccountDetail;

